//
//  TTEstimateDetailViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/13.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTEstimateDetailViewController.h"
#import "TTEstimateDetailTopView.h"
#import "TTEstimateDetailMoneyView.h"
#import "TTEstimateDetailInfoView.h"
#import <UIView+TYAlertView.h>
#import "TTEstimateImageView.h"
#import "TTSendButtonView.h"
#import "TTEstimateDetailModel.h"
#import "TTAutoEstimateDiamondListView.h"
#import "TTRecyclingSendViewController.h"
#import "TTEstimateMailInfoView.h"
#import "TTLookProgressViewController.h"
#import "TTRecyclingValuationViewController.h"
#import "TTRecyclingHomeRightNavbarViewManager.h"

@interface TTEstimateDetailViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) TTEstimateDetailTopView *topView;
@property (nonatomic, strong) TTEstimateMailInfoView *topMailView;
@property (nonatomic, strong) TTEstimateDetailMoneyView *moneyView;
@property (nonatomic, strong) TTEstimateDetailInfoView *infoView;
@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) TTEstimateDetailModel *detailModel;

@property (nonatomic, strong) TTAutoEstimateDiamondListView *autoDiamondView;

@property (nonatomic, assign) BOOL lastPageIsNeedReload;
/// 提示工作时间的view
@property (nonatomic, strong) UIView *hintTimeView;

@end

@implementation TTEstimateDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[TTRecyclingHomeRightNavbarViewManager sharedManager] showRightBarView];
    
    [self getDetailDatas];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"详情";
    self.view.backgroundColor = UIColorHex(#F7F7F7);
}

- (void)getDetailDatas
{
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:reclaimInformation_api parameters:@{@"param.reclaim_InformationId" : self.Reclaim_InformationId} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        self.detailModel = [TTEstimateDetailModel modelWithJSON:responseObject];
        
        [self setupSubviews];
        
        if (self.lastPageIsNeedReload) {
            self.lastPageIsNeedReload = NO;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteGoods_notify_key" object:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
    } showLoadingInView:self.view];
}

/// 删除估价记录
- (void)deleteGuJiaRecord
{
    NSDictionary *dic = @{
        @"param.reclaim_InformationId" : self.Reclaim_InformationId
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:recyclingDeleteRecord_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)

        [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteGoods_notify_key" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}

/// 自动转人工估价
- (void)autoChangeToPerson
{
    NSDictionary *dic = @{
        @"param.reclaim_InformationId" : self.detailModel.Reclaim_InformationId
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:recyclingChangingLabor_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        [self getDetailDatas];
        
        self.lastPageIsNeedReload = YES;
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}

/// 底部第一个按钮（删除）
- (void)bottomFirstBtnClick
{
    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"" message:@"您确定删除该条记录吗？" cancelTitle:@"取消" cancelBlock:^{
        
    } confirmTitle:@"确定" confirmBlock:^BOOL{
        
        [self deleteGuJiaRecord];
        return YES;
    }];
    
    [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:YES];
}

/// 底部第二个按钮
- (void)bottomsecondBtnClick
{
    int status = self.detailModel.Progress.intValue;
    if (status == 1 || status == 2 || status == 4) {
        // 回收
        TTRecyclingSendViewController *sendVC = [[TTRecyclingSendViewController alloc] init];
        sendVC.Reclaim_InformationId = self.Reclaim_InformationId;
        [self.navigationController pushViewController:sendVC animated:YES];
    } else {
        // 完善信息
        TTRecyclingValuationViewController *vc = [[TTRecyclingValuationViewController alloc] init];
        vc.Reclaim_InformationId = self.detailModel.Reclaim_InformationId;
        [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
    }
}

- (void)setupSubviews
{
    [self.view removeAllSubviews];
    
    _scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:_scrollView];
    CGFloat space = self.detailModel.Progress.intValue == 5 ? 0 : -70;
    CGFloat topSpace = self.detailModel.Progress.intValue == 2 ? 45 : 0;
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(space);
    }];
    
    _scrollView.contentInset = UIEdgeInsetsMake(topSpace, 0, 0, 0);
    if (self.detailModel.Progress.intValue == 2) {
        [_scrollView addSubview:self.hintTimeView];
        [self.hintTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.scrollView).mas_offset(-45);
            make.left.right.equalTo(self.view);
            make.height.equalTo(@35);
        }];
    }
    
    if (self.detailModel.Progress.intValue == 5) {
        _topMailView = [TTEstimateMailInfoView createViewFromNib];
        [self.scrollView addSubview:_topMailView];
        [_topMailView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.left.equalTo(self.view).mas_offset(15);
            make.right.equalTo(self.view).mas_offset(-15);
            make.height.mas_equalTo(110);
        }];
        
        [_topMailView setMailTopViewWithModel:self.detailModel];
        
    } else {
        _topView = [TTEstimateDetailTopView createTopViewWithModel:self.detailModel];
        [self.scrollView addSubview:_topView];
        @weakify(self)
        /// 转人工
        _topView.changeToPersonBlock = ^{
            @strongify(self)
            [self autoChangeToPerson];
        };
        /// 完善信息
        _topView.perfectInfoBlock = ^(NSString * _Nonnull reason) {
            @strongify(self)
            TTRecyclingValuationViewController *vc = [[TTRecyclingValuationViewController alloc] init];
            vc.Reclaim_InformationId = self.detailModel.Reclaim_InformationId;
            vc.reasonStr = reason;
            [[LLRouteTool getCurrentNavigationController] pushViewController:vc animated:YES];
        };
    }
    
    _moneyView = [TTEstimateDetailMoneyView createViewFromNib];
    _moneyView.money = self.detailModel.Price;
    [self.scrollView addSubview:_moneyView];
    [_moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.detailModel.Progress.intValue == 5) {
            make.top.equalTo(self.topMailView.mas_bottom).mas_offset(10);
        } else {
            make.top.equalTo(self.topView.mas_bottom).mas_offset(10);
        }
        make.left.mas_equalTo(15);
        make.right.equalTo(self.view).mas_offset(-15);
        make.height.mas_equalTo(80);
    }];
    
    _infoView = [TTEstimateDetailInfoView createViewFromNib];
    [_infoView setTitle:self.detailModel.OperateRecord_FrontContent time:self.detailModel.OperateRecord_CreateTime];
    [self.scrollView addSubview:_infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.moneyView.mas_bottom).mas_offset(10);
        make.left.right.equalTo(self.moneyView);
        make.height.mas_equalTo(70);
    }];
    @weakify(self)
    _infoView.lookProgress = ^{
        @strongify(self)
        TTLookProgressViewController *lookVC = [[TTLookProgressViewController alloc] initWithRecyclingNum:self.Reclaim_InformationId];
        [self.navigationController pushViewController:lookVC animated:YES];
    };
    
    
    if (self.detailModel.CurrentType.intValue == 1) {
        [self showManRecyclingImageView];
    } else {
        [self showAutoRecyclingImageView];
    }
    
    
    TTSendButtonView *btnView = [TTSendButtonView createSendButtonViewAndClickSendButtonBlock:^{
        @strongify(self)
        [self bottomFirstBtnClick];
    } startValuationBtnBlock:^{
        @strongify(self)
        [self bottomsecondBtnClick];
    }];
    if (self.detailModel.Progress.intValue == 1 || self.detailModel.Progress.intValue == 2) {
        [btnView setLeftBtnTitle:@"删除" rightBtnTitle:@"寄货回收"];
    } else if (self.detailModel.Progress.intValue == 3) {
        [btnView setLeftBtnTitle:@"删除" rightBtnTitle:@"去完善"];
    } else if (self.detailModel.Progress.intValue == 4) {
        [btnView setLeftBtnTitle:@"删除" rightBtnTitle:@"认可估价，我要回收"];
    } else {
        btnView.hidden = YES;
    }
    [btnView setButtonEnabled:YES];
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_bottom).mas_offset(10);
        make.height.equalTo(@40);
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
    }];
    
    
}


/// 人工估价的view
- (void)showManRecyclingImageView
{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 10;
    [self.scrollView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoView.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view).mas_offset(12);
        make.right.equalTo(self.view).mas_offset(-12);
        make.height.mas_equalTo(200);
    }];
    
    UILabel *hintLabel = [UILabel labelWithTitle:@"实物/证书图片" color:UIColorHex(#333333) fontSize:13];
    [_bgView addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@13);
        make.left.mas_equalTo(12);
    }];
    
    [self.view layoutIfNeeded];
    
    CGFloat bottom = hintLabel.bottom + 10;
    int i = 0;
    for (TTEstimateDetailRecordModel *model in self.detailModel.Information_Record_List) {
        
        TTEstimateImageView *esImgView = [TTEstimateImageView createViewWithFrame:CGRectMake(0, bottom, self.bgView.width, 100) model:model];
        [self.bgView addSubview:esImgView];
        [self.view layoutIfNeeded];
        bottom = esImgView.bottom + 10;
        if (i == self.detailModel.Information_Record_List.count - 1) {
            esImgView.lineHidden = YES;
        }
        i++;
    }
    
    [_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(bottom);
    }];
    
    
    [self.view layoutIfNeeded];
    [self.scrollView setContentSize:CGSizeMake(0, self.bgView.bottom + 50)];
    
    
}

/// 显示自动估价的view
- (void)showAutoRecyclingImageView
{
    self.autoDiamondView = [[TTAutoEstimateDiamondListView alloc] init];
    self.autoDiamondView.lookZSBtn.hidden = YES;
    [self.scrollView addSubview:self.autoDiamondView];
    [_autoDiamondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(15);
        make.right.equalTo(self.view).mas_offset(-15);
        make.height.mas_equalTo(200);
    }];
    
    NSMutableArray *array = [NSMutableArray array];
    TTReclaimDiamondsInformationModel *model = self.detailModel.Reclaim_Information_Diamonds_AppendInformation;
    [array addObject:@{@"leftTitle" : @"证书编号", @"rightTitle" : model.ReportNo}];
    [array addObject:@{@"leftTitle" : @"形状", @"rightTitle" : model.Shape}];
    [array addObject:@{@"leftTitle" : @"克拉重量", @"rightTitle" : model.DiaSize}];
    [array addObject:@{@"leftTitle" : @"颜色等级", @"rightTitle" : model.Color}];
    [array addObject:@{@"leftTitle" : @"净度等级", @"rightTitle" : model.Clarity}];
    [array addObject:@{@"leftTitle" : @"切工等级", @"rightTitle" : model.Cut}];
    [array addObject:@{@"leftTitle" : @"抛光", @"rightTitle" : model.Polish}];
    [array addObject:@{@"leftTitle" : @"对称", @"rightTitle" : model.Sym}];
    [array addObject:@{@"leftTitle" : @"荧光", @"rightTitle" : model.Flour}];
    [array addObject:@{@"leftTitle" : @"肉眼净", @"rightTitle" : model.EyeClean}];
    [array addObject:@{@"leftTitle" : @"奶色", @"rightTitle" : model.Milky}];
    [array addObject:@{@"leftTitle" : @"咖色", @"rightTitle" : model.Browness}];
    [array addObject:@{@"leftTitle" : @"绿色", @"rightTitle" : model.Green}];
    [array addObject:@{@"leftTitle" : @"黑色", @"rightTitle" : model.Black}];
    
    self.autoDiamondView.lookZSBtn.hidden = model.ReportNo.length <= 1;
    [self.autoDiamondView setDataArray:array certUrlString:model.CertFile];
    
    
    [self.view layoutIfNeeded];
    [self.scrollView setContentSize:CGSizeMake(0, self.autoDiamondView.bottom + 50)];
    
    self.autoDiamondView.clickLookZsBlock = ^{
        [[TTRecyclingHomeRightNavbarViewManager sharedManager] hiddenRightBarView];
    };
    
}


- (UIView *)hintTimeView
{
    if (!_hintTimeView) {
//    WithFrame:CGRectMake(0, 0, kScreenWidth, 35)
        _hintTimeView = [[UIView alloc] init];
        _hintTimeView.backgroundColor = UIColorHex(#FDF4C8);
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"laba"]];
        imageView.frame = CGRectMake(0, (35 - 20)*0.5, 30, 20);
        imageView.contentMode = UIViewContentModeCenter;
        imageView.backgroundColor = _hintTimeView.backgroundColor;
        [_hintTimeView addSubview:imageView];
        
        UILabel *label = [UILabel labelWithTitle:@"现在为非工作时间，人工估价时间为周一至周五9:00-18:00" color:UIColorHex(#FF5500) fontSize:13];
        [label sizeToFit];
        label.frame = CGRectMake(30, 0, label.width, 35);
        [_hintTimeView insertSubview:label belowSubview:imageView];
        
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        anim.fromValue = @(0);
        anim.toValue = @(label.width + 20 - kScreenWidth);
        anim.duration = 2;
        anim.repeatCount = MAXFLOAT;
        anim.autoreverses = YES;
        anim.removedOnCompletion = NO;
        anim.fillMode = kCAFillModeForwards;
        [label.layer addAnimation:anim forKey:nil];
        
    }
    return _hintTimeView;
}

@end
